<template>
    <div class="login">
        <div class="logincon">
            <div class="top">
                <h1>hello</h1>
            </div>
            <div class="login-from">
                <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px"
                    class="demo-ruleForm">
                    <el-form-item label="账号" prop="name">
                        <el-input type="password" v-model="ruleForm.name" autocomplete="off"
                            prefix-icon="el-icon-s-custom"></el-input>
                    </el-form-item>
                    <el-form-item label="密码" prop="Pass">
                        <el-input type="password" v-model="ruleForm.Pass" autocomplete="off" prefix-icon="el-icon-lock">
                        </el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="submitForm()">登录</el-button>
                        <el-button @click="resetForm()">重置</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
    import {
        login
    } from "@/network/login.js"
    export default {
        name: "Login",
        data() {
            return {
                ruleForm: {
                    name: '',
                    Pass: '',
                },
                rules: {
                    name: [{
                            required: true,
                            message: '请输入用户名称',
                            trigger: 'blur'
                        },
                        {
                            min: 5,
                            max: 12,
                            message: '长度在 6到 12 个字符',
                            trigger: 'blur'
                        }
                    ],
                    Pass: [{
                            required: true,
                            message: '请输入密码',
                            trigger: 'blur'
                        },
                        {
                            min: 6,
                            max: 12,
                            message: '长度在 6-12个字符',
                            trigger: 'blur'
                        }
                    ],
                }
            }
        },
        methods: {
            resetForm() {
                this.ruleForm.name = ""
                this.ruleForm.Pass = ""
            },
            // 密码验证成功触发的函数
            submitForm() {
                if (this.ruleForm.name == "admin" && this.ruleForm.Pass == "123456") {
                    // this.$router.push({path:'/home',query:{id:1}})
                    this.loginM()
                } else {
                    this.$message({
                        showClose: true,
                        message: '您的用户名和密码不正确',
                        type: 'error'
                    });
                }
            },
            loginM() {
                login("/login", {
                    name: this.ruleForm.name,
                    pass: this.ruleForm.Pass
                }).then(res => {
                    // console.log(res);
                    //把token值放到本地存储
                    let token = res.data.token
                    sessionStorage.setItem("token", token)
                    this.$message({
                        showClose: true,
                        message: '正在跳转',
                        type: 'success'
                    });
                    this.$router.push("/home")
                })
            }
        }

    }
</script>

<style scoped lang="less">
    .login {
        width: 100%;
        height: 100%;
        background: url("~@/assets/img/bg.jpg");
        background-size: cover;
        position: relative;

        .logincon {
            position: absolute;
            width: 600px;
            top: 269px;
            right: 20px;
        }
    }
</style>